<?php 
namespace addons\prl\model;
use app\admin\model\prl\Channels as ChannelsAdminModel;
/**
 * 
 */
class Channels extends ChannelsAdminModel
{
	protected function scopeHome($q)
	{
		$q->where('type',0)->where('status', 1);
	}

	protected function scopeChannel($q, $id = null)
	{
        if($id != null){
            $q->where('id', $id);
        }
	}

    protected function scopeNomal($q)
    {
        $q->where('status', 1);
    }

	protected function scopeChannelorder($q)
	{
		$q->order('weigh,id');
	}

	public function getHrefAttr($value, $data)
    {
        $config = get_addon_config('blcms');
        if(empty($config)){ //如果没有获取到插件信息 默认设置一个
        	$config['rewrite']['channels/index'] = '/channel/[:id]';
        	$config['rewrite']['lists/index'] = '/list/[:id]';
        	$config['rewrite']['pages/index'] = '/page/[:id]';
        	$config['rewrite']['forms/index'] = '/form/[:id]';
        }

        $href = '';
        $id = $data['id'];
        switch ($data['type']) {
            case '0':
                $href = '/';
                break;
            case '1' :
                $href = str_replace("[:id]" ,$id ,$config['rewrite']['channels/index']) ;
                break;
            case '2' :
                $href = str_replace("[:id]" ,$id ,$config['rewrite']['lists/index']) ;
                break;
            case '4' :
                $href = str_replace("[:id]" ,$id ,$config['rewrite']['pages/index']) ;
                break;
            case '5' :
                $href = str_replace("[:id]" ,$id ,$config['rewrite']['forms/index']) ;
                break;
            default:
                $href = isset($data['url']) ? $data['url'] : '';
                break;
        }
        return  $href;
    }

    public function articles()
    {
        return $this->hasMany('\\addons\\prl\\model\\Articles');
    }

    public function models()
    {
        return $this->hasOne('Models', 'id', 'models_id');
    }

    public function forms()
    {
        return $this->hasOne('Forms', 'id', 'form_id');
    }
}